﻿;/**
** custom Formater for edit and delete button in jqgrid
**/
; editFormatter = function (cellValue, options, rowobject) {
    return '<a href="#" class="ui-grid-edit-button" data="' + options.rowId + '"><span class="glyphicon glyphicon-pencil"></span></a>';
}; deleteFormatter = function (cellValue, options, rowobject) {
    return '<a href="#" class="ui-grid-delete-button" data="' + options.rowId + '"><span class="glyphicon glyphicon-trash"></span></a>';
};
statusFormatter = function(cellValue, options, rowobject) {
    //  Pending = 0,
    //  AsmApproved = 1,
    //  AsmRejected = 2,
    //  AdminApproved = 3,
    //  AdminRejected = 4,
    //  DeletePending =5,
    //  DeleteAsmApproved = 6
    var statusClass = 'pending';
    
    if (cellValue == 'AdminApproved') {
        statusClass = 'adminapprove';
    }
    return '<div style="display:inline-block;width:100%;"><span style="display:inline-block; float:left;">' + cellValue + '</span>' + '<span class="status ' + statusClass + '"></span></div>';
};
;var callGrid = function ($gridEl, opts) {
    var self = this;
    self.defaults = {
        colId: 'id',
        deleteUrl: '/Delete',
        ajaxType: 'POST',
        deleteModal: '#delete-dialog'
    };

    self.opts = $.extend(this.defaults, opts);
    self.$grid = $gridEl;
};
callGrid.prototype.gridLoadCompleted = function () {
    var self = this;
    $(self.$grid.selector + ' .ui-grid-edit-button').each(function (index, item) {
        $(item).on('click', function () {
            var row = self.$grid.getRowData($(item).attr('data'));
            //alert(row[self.opts.rowColumnId]);
            var selectedId = row[self.opts.colId];
            onCreate(selectedId);
        });
    });

    $(self.$grid.selector + ' .ui-grid-delete-button').each(function (index, item) {
        $(item).on('click', function () {
            var row = self.$grid.getRowData($(item).attr('data'));
            var selectedId = row[self.opts.colId];
            $(self.opts.deleteModal).modal('show');
            $(self.opts.deleteModal + ' #btnDelete').unbind('click');
            $(self.opts.deleteModal + ' #btnDelete').on('click', function () {
                //alert('delete di pa!!!!');
                $.ajax({
					url: self.opts.deleteUrl,
					data: {id : selectedId},
					type: 'POST',
					dataType: 'json',
					success: function(data){
						if(data.success){
							$(self.opts.deleteModal).modal('hide');
							self.$grid.trigger('reloadGrid');
						}else{
							// TODO: show the error message to user
						}
					},
					error: function(respText){
						// TODO: show the error message to user
					}
				});
            });
        });
    });
};